﻿using Amazon;
using  Amazon.S3;
using Amazon.S3.IO;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AWS_FTP.Utilities;
using Amazon.Runtime;

namespace AWS_FTP
{
    /// <summary>
    /// Interaction logic for FileExplorer.xaml
    /// </summary>
    public partial class FileExplorer : Page
    {
        private AmazonS3 s3 = null;
        private AWSCredentials aws_credentials = null;

        public FileExplorer()
        {
            InitializeComponent();
            aws_credentials = Application.Current.Resources[ResourceKeys.AUTH] as AWSCredentials;
            s3 = AWSClientFactory.CreateAmazonS3Client(aws_credentials);
            Application.Current.Resources.Add(ResourceKeys.S3, s3);        }

        private void FileExplorer_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateUI();
        }

        private IEnumerable<S3Bucket> GetAllBuckets()
        {
            try
            {                
                return s3.ListBuckets().Buckets;
            }
            catch (Exception aws)
            {
                AWSLogger.Write("Failed to fetch available buckets.", aws);
                return null;
            }
        }

        private void ExploreBucket(S3Bucket bucket)
        {
            try
            {
                ListObjectsRequest request = new ListObjectsRequest();
                request.BucketName = bucket.BucketName;
                ListObjectsResponse response = s3.ListObjects(request);                
                DeleteObjects(request.BucketName,GetKeyVersions<S3Object>(response.S3Objects.ToList()));
            }
            catch (Exception aws)
            {
                AWSLogger.Write("Failed to explore buckets.", aws);
            }
        }

        private List<KeyVersion> GetKeyVersions<T>(List<T> objects)
        {
            List<KeyVersion> keyversions = new List<KeyVersion>();
            foreach (var key in objects)
            {
                keyversions.Add(new KeyVersion(key.ToString()));
            }
            return keyversions;
        }

        private void DeleteObjects(string bucketName, List<KeyVersion> keys = null)
        {
            try
            {
                DeleteObjectsRequest request = new DeleteObjectsRequest();
                request.BucketName = bucketName;
                List<KeyVersion> keyversions = new List<KeyVersion>();                
                if (keys != null)
                {
                    keyversions.AddRange(keys);
                }
                request.WithKeys(keyversions.ToArray());
                DeleteObjectsResponse response = new DeleteObjectsResponse();
                response.DeletedObjects = s3.DeleteObjects(request).DeletedObjects;
            }
            catch (Exception aws)
            {
                AWSLogger.Write("Failed to delete objects in the bucket.", aws);
            }
        }

        private void UpdateUI()
        {
            try
            {
                lstBuckets.ItemsSource = GetAllBuckets();
                lstBuckets.DisplayMemberPath = "BucketName";
                lstBuckets.SelectedIndex = -1;
                //cmbBuckets.ItemsSource = GetAllBuckets();
                //cmbBuckets.DisplayMemberPath = "BucketName";
                //cmbBuckets.SelectedIndex = -1;
            }
            catch (Exception aws)
            {
                AWSLogger.Write("Failed to update explorer UI.", aws);
            }
        }

        private void DropBox_Drop(object sender, DragEventArgs e)
        {

        }

        private void DropBox_DragEnter(object sender, DragEventArgs e)
        {

        }

        private void DropBox_DragOver(object sender, DragEventArgs e)
        {

        }

        private void DropBox_DragLeave(object sender, DragEventArgs e)
        {

        }     
    }
}